import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/home/demo.vue'
import login from '../views/login/index.vue'
import regies from '../views/regies/index.vue'
import code from '@/views/code/index.vue'
import goods from '@/views/goods/index.vue'
// 二级路由
import cart from '@/views/home/children/cart.vue'
import index from '@/views/home/children/index.vue'
import my from '@/views/home/children/my.vue'
import cla from '@/views/home/children/cla.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/',
      redirect:'/home/index'
    },
    {
      path:'/login',
      name:'login',
      component:login
    },
    {
      path:'/goods/:goods_id',
      name:'goods',
      component:goods
    },
    {
      path:'/code/:query',
      name:'code',
      component:code
    },
    {
      path:'/regies',
      name:'regies',
      component:regies
    },
    {
      path: '/home',
      name: 'home',
      component: HomeView,
      children:[
        {
          path:'cart',
          name:'cart',
          component:cart
        },
        {
          path:'index',
          name:'index',
          component:index
        },
        {
          path:'my',
          name:'my',
          component:my
        },
        {
          path:'cla',
          name:'cla',
          component:cla
        },
      ]
    },
    // {
    //   path: '/about',
    //   name: 'about',
    //   // route level code-splitting
    //   // this generates a separate chunk (About.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () => import('../views/AboutView.vue'),
    // },
  ],
})

router.beforeEach((to,from,next)=>{
  let token=localStorage.getItem('token')
  if(to.fullPath=='/home/cart'||to.fullPath=='/home/my'){
    if(!token){
      next('/login')
    }else{
      next()
    }
  }else{
    next()
  }
})

export default router
